export class NameTable {
    private width: number;
    private height: number;
    private name: string;
    private attrib: number[];

    public tile: number[];

    constructor (width: number, height: number, name: string) {
        this.width = width;
        this.height = height;
        this.name = name;

        this.tile = new Array(width * height);
        this.attrib = new Array(width * height);
    }

    getTileIndex (x: number, y: number) {
        return this.tile[y * this.width + x];
    }

    getAttrib (x: number, y: number) {
        return this.attrib[y * this.width + x];
    }

    writeAttrib (index: number, value: number) {
        const basex = (index % 8) * 4;
        const basey = Math.floor(index / 8) * 4;
        let add: number;
        let tx: number, ty: number;
        let attindex: number;

        for (let sqy = 0; sqy < 2; sqy++) {
            for (let sqx = 0; sqx < 2; sqx++) {
                add = (value >> (2 * (sqy * 2 + sqx))) & 3;
                for (let y = 0; y < 2; y++) {
                    for (let x = 0; x < 2; x++) {
                        tx = basex + sqx * 2 + x;
                        ty = basey + sqy * 2 + y;
                        attindex = ty * this.width + tx;
                        this.attrib[ty * this.width + tx] = (add << 2) & 12;
                    }
                }
            }
        }
    }
}
